package com.questionnaire.controller;


import com.questionnaire.model.User;
import com.questionnaire.result.ResultUtil;
import com.questionnaire.service.IUserService;
import com.questionnaire.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户问卷调查
 *
 * @author lj
 */
@Controller
@RequestMapping("/index")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {

        return "toup.html";
    }


    /**
     * 添加问卷
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Object insert(@RequestBody User user) {
        if (ToolUtil.isEmpty(user.getAnsweList())) {
            return ResultUtil.error("系统异常请尝试重新填写");
        }
        if (ToolUtil.isEmpty(user.getClerk())) {
            return ResultUtil.error("业务员不能为空");
        }
       /* if (ToolUtil.isEmpty(user.getCompany())) {
            return ResultUtil.error("店名不能为空");
        }
        if (ToolUtil.isEmpty(user.getName())) {
            return ResultUtil.error("姓名不能为空");
        }
        if (ToolUtil.isEmpty(user.getPhone())) {
            return ResultUtil.error("手机号不能为空");
        }*/
        //通过手机号判断是否已经填写过
        if (ToolUtil.isNotEmpty(user.getPhone())) {
            User check = userService.checkByPhone(user.getPhone());
            if (check != null) {
                return ResultUtil.error("该手机号已经填写过，请勿重复操作");
            }
        }
        int i = userService.save(user);
        if (i != 1) {
            return ResultUtil.error("系统异常请尝试重新填写");
        }
        return ResultUtil.success("添加成功");
    }

}
